package com.ftg.learn.system.controller;

import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import java.io.ByteArrayInputStream;
/**
 * 上传控制器
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/22 上午8:19
 */
@RestController
public class UploadController {

    /**
     * file -- File F = new File("c://aa.txt");
     * FileInputStream fis;
     *
     * springboot
     *   springframework :
     *      web : 文件的流
     *          MultipartFile
     * @param file   前端选择的文件流
     * @return
     */

//    @PostMapping("/upload")
//    public String upload(@RequestParam("imgdesc")String imgdesc,@RequestParam("file")MultipartFile file) throws IOException {
//        //上传的文件类型   任意类型   原始文件后缀就应该新文件后缀
//
//        System.out.println(file.getName()+"参数名称");
//        System.out.println(file.getContentType()+"有没有可能是类型");
//        System.out.println(file.getBytes().length +"字节数");
//        System.out.println(file.getSize()+"大小");
//
//        System.out.println(file.getOriginalFilename()+"是否为文件名称");
//
//        try {
//            String fileName = file.getOriginalFilename();
//            String suffix = fileName.substring(fileName.lastIndexOf("."));
//
//            String newFile = UUID.randomUUID()+suffix;
//
//            file.transferTo(new File("/home/kangjx/Desktop/"+newFile));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//
//        return "success";
//    }

    @PostMapping("/upload")
    public String upload(@RequestParam("imgdesc")String imgdesc,@RequestParam("file")MultipartFile file) throws IOException {
        //上传的文件类型   任意类型   原始文件后缀就应该新文件后缀

        System.out.println(file.getName()+"参数名称");
        System.out.println(file.getContentType()+"有没有可能是类型");
        System.out.println(file.getBytes().length +"字节数");
        System.out.println(file.getSize()+"大小");

        System.out.println(file.getOriginalFilename()+"是否为文件名称");

        try {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));

            String newFile = UUID.randomUUID()+suffix;

            // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
            String endpoint = "https://oss-cn-beijing.aliyuncs.com";

           String accessId = "LTAI5t77VU5jTCCMKADa9Ny5 1231321";
           String accessKey = "eoalbgg8sf7SexfGbAZ2YPcHunEcuX 1231321";

            String bucketName = "pro-img-list";

            String objectName = newFile;

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessId,accessKey);

            try {
                //把文件 流存储 到云里面
                ossClient.putObject(bucketName, objectName, file.getInputStream());
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return "success";
    }



    //1 : 大小可能调整吗？ 如果可以，在哪里调整？

    //2 ： 类型拦截？ 怎么做?



//    用户登录名称 demo123@1947492684630045.onaliyun.com
//    AccessKey ID LTAI5t77VU5jTCCMKADa9Ny5
//    AccessKey Secret eoalbgg8sf7SexfGbAZ2YPcHunEcuX

}
